package triTree;

import transaction.*;
import triDataStruct.*;
import triFileSystem.*;
import triStorage.*;

public class InsertableTriSet extends BufferObject {

	private int inodePageId;
	
	private PageDir inode;
	
	private IWriter writer;
	
	//private RWLock lock;
	
	
	public InsertableTriSet() {
		super();
	}

	public void reset(int inodePageId, PageDir inode, IWriter writer) {
		if (this.writer != null)
			this.writer.dispose();
		this.inodePageId = inodePageId;
		this.inode = inode;
		this.writer = writer;
		
		//lock = new RWLock();
	}
	
	public int getInodePageId() {
		return inodePageId;
	}
	
	@Override
	public int getKey() {
		return inodePageId;
	}
	
	public PageDir getInodeSnapshot(Transaction tx) {
		return inode.copy(tx);
	}
	
	public long getFilePos(Transaction tx) {
		lock.lockRead(tx);
		try {
			return writer.getWritePos();
		}
		finally {
			lock.unlockRead(tx);
		}
		
	}
	
	public void insertItem(String item, Transaction tx) {
		lock.lockWrite(tx);
		try {
			writer.appendInt(1, tx);
			writer.appendString(item, tx);
		}
		finally {
			lock.unlockWrite(tx);
		}
	}
	
	public void dispose() {
		writer.dispose();
	}
	
}
